if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");


function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!")
    while (true) {
      var allow = text("允许").findOne(5000)
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!")
        allow.click()
        sleep(1000)
        thread.interrupt()
        break
      }
    }
  })
  sleep(1000)
  var result = launchApp(app_name)
  sleep(3000)
  thread.interrupt()
  return result
}

// 随机向上滑动
function swip_up() {
  var w = device.width
  var h = device.height

  function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = { "x": 0, "y": 0 };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
  };


  function sml_move(qx, qy, zx, zy, time) {
    //仿真随机带曲线滑动: qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
    var xxy = [time];
    var point = [
      { "x": qx, "y": qy },
      { "x": random(qx - 100, qx + 100), "y": random(qy, qy + 50) },
      { "x": random(zx - 100, zx + 100), "y": random(zy, zy + 50) },
      { "x": zx, "y": zy }
    ];
    for (let i = 0; i < 1; i += 0.08) {
      xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]
      xxy.push(xxyy);
    }
    gesture.apply(null, xxy);
  }

  sml_move(w / 2, h * 0.8 + h * random() * 0.1, w / 2, h * 0.15, 200 + 600 * random());
}

function task() {
  var 确定 = className('android.widget.Button').findOne(20000)
  if (确定) {
    sleep(1000)
    确定.click()
  }
  var 首页 = text('首页').findOne(5000)
  if (首页) {
    sleep(5000)
    var index = 1
    var count = 10
    while (index <= count) {
      var random_time = random(6000, 8000)
      toastLog("观看第" + index + "/" + count + "个视频中...")
      sleep(time)
      toastLog("预计时长" + (random_time / 1000) + "秒~")
      sleep(random_time)
      swip_up()
      index++
      if(index*25 < 100){
        utils.updateAppProgress(index*25);
      }
      var 首页 = text("首页").findOne(1000);
      if (!首页) {
        sleep(time)
        break;
      }
    }
    //我的
    var 我的  = text("我的").findOne(1000)
    if(我的){
      我的.click();
      sleep(1000)
      click(592,85,656,181) 
      sleep(1000)
      var 能量值 = text("能量值+种子").findOne(1000)
      if(能量值){
        能量值.click()
      }
    }
  }
}

function main() {
  let open_app = launchAppEnhance("乡拍");
  if (open_app) {
    task()
  }
  else {
    toastLog("乡拍打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}

storage.put("script_running_status", "end");
